package com.boshao.classschedule.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.boshao.classschedule.R;
import com.boshao.classschedule.adapter.MainFragmentAdapter;
import com.boshao.classschedule.model.classschedule.ClassSchedule;
import com.boshao.classschedule.model.classschedule.ClassScheduleDao;
import com.boshao.classschedule.widget.ActionBarLeft;
import com.boshao.classschedule.widget.ActionBarRight;

/**
 * 添加课程Fragment
 * @author boshao
 *
 */
public class AddOrModifyClassFragment extends AbstartFragment implements OnClickListener{
	public static final String TAG = "add_or_modify_class_fragment";
	View mRootView;
	EditText mEtclassName,mEtTeacherName,mEtAdress;
	static TextView mTvWeek,mTvTime;
	private static final int NEW = 0; 
	private static final int MODIFY = 1; 
	private static int sStyle;
	private ClassScheduleFragment mClassScheduleFragment;
	private ClassSchedule mClassSchedule;
	private ViewPager mViewPager;
	
	/**
	 * 创建修改课程时的Fragment对象
	 * @param classScheduleFragment
	 * @param classSchedule
	 * @return
	 */
	public static AddOrModifyClassFragment newInstance(ClassScheduleFragment classScheduleFragment,ClassSchedule classSchedule){
		AddOrModifyClassFragment fragment = new AddOrModifyClassFragment();
		fragment.setClassSchedule(classSchedule);
		fragment.setClassScheduleFragment(classScheduleFragment);
		sStyle = MODIFY;
		return fragment;
	}
	
	/**
	 * 创建添加课程时的Fragment对象
	 * @param viewPager
	 * @return
	 */
	public static AddOrModifyClassFragment newInstance(ViewPager viewPager){
		AddOrModifyClassFragment fragment = new AddOrModifyClassFragment();
		fragment.setViewPager(viewPager);
		sStyle = NEW;
		return fragment;
	}
	
	private void setClassScheduleFragment(ClassScheduleFragment classScheduleFragment){
		mClassScheduleFragment = classScheduleFragment;
	}
	
	private void setClassSchedule(ClassSchedule classSchedule){
		mClassSchedule = classSchedule;
	}
	
	private void setViewPager(ViewPager viewPager){
		mViewPager = viewPager;
	}
	
	@Override
	public String getFragmentTag() {
		return TAG;
	}
	
	@Override
	protected int getContentLayoutId() {
		return R.id.rl_add_class;
	}
	
	//需要 带返回的Actionbar
	@Override
	protected ActionBarLeft getActionBarLeft() {
		return ActionBarLeft.BACK;
	}
	
	@Override
	protected void onClickActionLeft() {
		onBackPressed();//退出当前fragment
	}
		
	@Override
	protected ActionBarRight getActionBarRight() {
		return ActionBarRight.CHECK;
	}
	
	@Override
	protected void onClickActionRight(int resId) {
		if(resId == R.id.action_right_one){//点击第一个右边action的事件监听(从左到右)
			ClassScheduleDao dao = readClassScheduleDao();
			Toast.makeText(getActivity(),"删除课程成功",Toast.LENGTH_LONG).show();
			dao.delete(mClassSchedule);
			if(mClassScheduleFragment != null){
				mClassScheduleFragment.refrashView();
			}
			onBackPressed();
		}else if(resId == R.id.action_right_two){//第二个事件监听
			saveClassSchedule();
		}
	}
	
	/**
	 * 添加或修改课程
	 */
	private void saveClassSchedule() {
		if(sStyle == NEW){
			if(isChange()){
				ClassScheduleDao dao = writeClassScheduleDao();
				ClassSchedule clSchedule = new ClassSchedule(null,getClassName(),getTeacherName(), 
						getWeek(),getTime(),getAdress());
				dao.insert(clSchedule);
				Toast.makeText(getActivity(),"创建课程成功",Toast.LENGTH_LONG).show();
			}else{
				Toast.makeText(getActivity(),"请输入日程名字和开始日期和时间",Toast.LENGTH_LONG).show();
			}
			Fragment f = null;
			if(mViewPager != null){
				f = ((MainFragmentAdapter)mViewPager.getAdapter()).getItem(1);
			}
			if(f != null){
				((ClassScheduleFragment)f).refrashView();
			}
		}else if(sStyle == MODIFY){	
			if(isChange() && mClassSchedule != null){
				ClassScheduleDao dao = readClassScheduleDao();
				ClassSchedule clSchedule = new ClassSchedule(mClassSchedule.getId(),getClassName(),getTeacherName(), 
						getWeek(),getTime(),getAdress());
				try{
					dao.update(clSchedule);
				}catch(Exception e){
				}
				Toast.makeText(getActivity(),"修改日程成功",Toast.LENGTH_LONG).show();
				
			}else{
				Toast.makeText(getActivity(),"请输入日程名字和开始日期和时间",Toast.LENGTH_LONG).show();
			}
			if(mClassScheduleFragment != null){
				mClassScheduleFragment.refrashView();
			}
		}
		onBackPressed();
	}
	
	
	@Override
	protected String getActionBarLeftTitle() {
		return sStyle == NEW ? "创建日程" : "修改日程";
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		View view = super.onCreateView(inflater, container, savedInstanceState);
		View delete = view.findViewById(R.id.action_right_one);
		if(sStyle == NEW){//如果是添加课程，就隐藏删除图标
			delete.setVisibility(View.GONE);
		}else{
			delete.setVisibility(View.VISIBLE);
		}
		return view;
	}
	
	@Override
	protected View onAppCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		mRootView = inflater.inflate(R.layout.fragment_add_class,container,false);	
		mEtclassName = (EditText) mRootView.findViewById(R.id.et_class_name);
		mEtTeacherName = (EditText) mRootView.findViewById(R.id.et_teach_name);
		mTvWeek = (TextView) mRootView.findViewById(R.id.et_class_week);
		mTvWeek.setOnClickListener(this);
		mTvTime = (TextView) mRootView.findViewById(R.id.et_class_time);
		mTvTime.setOnClickListener(this);
		mEtAdress = (EditText) mRootView.findViewById(R.id.et_class_adress);
		
		initData();
		return mRootView;
	}

	/**
	 * 初始化数据
	 */
	private void initData() {
		if(mClassSchedule != null){
			mEtclassName.setText(mClassSchedule.getClassName());
			mEtTeacherName.setText(mClassSchedule.getTeacherName());
			mTvWeek.setText(mClassSchedule.getSelectedWeek());
			mTvTime.setText(mClassSchedule.getSelectedTime());
			mEtAdress.setText(mClassSchedule.getAddress());
		}
	}

	/**
	 * 判断是否输入日程名称、时间
	 */
	public boolean isChange(){
		boolean a = !TextUtils.isEmpty(getClassName());
		boolean b = !TextUtils.isEmpty(getWeek());
		boolean c = !TextUtils.isEmpty(getTime());
		return a && b && c;
	}
	
	@Override
	protected boolean onBackPressed() {
		hideSoftInput(mRootView.getWindowToken());
		return super.onBackPressed();
	}
	
	public String getClassName(){
		return mEtclassName.getText().toString();
	}
	
	public String getTeacherName(){
		return mEtTeacherName.getText().toString();
	}
	
	public String getWeek(){
		return mTvWeek.getText().toString();
	}
	
	public String getAdress(){
		return mEtAdress.getText().toString();
	}
	
	public String getTime(){
		return mTvTime.getText().toString();
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.et_class_week:
			showWeekDialog();
			break;
		case R.id.et_class_time:
			showTimeDialog();
			break;
		}
	}
	
	/**
	 * 显示dialog选择周
	 */
	private void showWeekDialog() {
		WeeksDialogFragment.newInstance(this).show(getActivity().getSupportFragmentManager(),
				WeeksDialogFragment.TAG);
	}
	
	/**
	 * 显示dialog选择课时
	 */
	private void showTimeDialog(){
		ClassTimeFragment.newInstance(this).show(getActivity().getSupportFragmentManager(),ClassTimeFragment.TAG);
	}
}
